<?php

function formatCategories($categories, $parentId) {
    // $navCat stores all children categories
    // of $parentId
    $navCat = array();

    // expand only the categories with the same parent id
    // all other remain compact
    $ids = array();
    foreach ($categories as $category) {
        if ($category['category_parent'] == $parentId) {
            $navCat[] = $category;
        }

        // save the ids for later use
        $ids[$category['category_id']] = $category;
    }

    $tempParentId = $parentId;

    // keep looping until we found the
    // category where the parent id is 0
    while ($tempParentId != 0) {
        $parent = array($ids[$tempParentId]);
        $currentId = $parent[0]['category_id'];

        // get all categories on the same level as the parent
        $tempParentId = $ids[$tempParentId]['category_parent'];
        foreach ($categories as $category) {
            // found one category on the same level as parent
            // put in $parent if it's not already in it
            if ($category['category_parent'] == $tempParentId && !in_array($category, $parent)) {
                $parent[] = $category;
            }
        }

        // sort the category alphabetically
        array_multisort($parent);

        // merge parent and child
        $n = count($parent);
        $navCat2 = array();
        for ($i = 0; $i < $n; $i++) {
            $navCat2[] = $parent[$i];
            if ($parent[$i]['category_id'] == $currentId) {
                $navCat2 = array_merge($navCat2, $navCat);
            }
        }

        $navCat = $navCat2;
    }


    return $navCat;
}

// get all categories
//$categories = fetchCategories();
// format the categories for display
//echo $categoryId;
$categories = formatCategories($aryCategory, $categoryId);
//print_r($categories);
?>
<div class="right-nav">
    <ul>
        <?php
        foreach ($categories as $category) {
            extract($category);

            if ($category_level == 0) {
                $category_name = '&nbsp;&nbsp;' . $category_name;
            }
            if ($category_level == 1) {
                $category_name = '&nbsp; &nbsp; &nbsp;&nbsp;' . $category_name;
            }
            if ($category_level == 2) {
                $category_name = '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;' . $category_name;
            }
            if ($category_level == 3) {
                $category_name = '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;' . $category_name;
            }
            if ($category_total_child > 0) {
                $category_name .= '<span><img src="/public/images/form/arrow_state_blue_right.png"></span>';
            }

            $listId = '';
            if ($category_id == $categoryId) {
                $listId = 'selected';
            }
        ?>

            <li class="cpointer <?php echo $listId; ?>"><a onclick="AdminController.getListProductMaker('<?php echo $orderType; ?>', '<?php echo $category_id; ?>', '<?php echo $page; ?>')"><?php echo $category_name; ?></a></li>


        <?php } ?>

    </ul>
</div>




<?php if ($selectId == 1 && $spectId == 0) { ?>
    <span>Nhập tên thông số</span>
    <span><input type="text" name="specs_name[]" class="w200p"></span>
    <span><a class="cpointer" onclick="AdminController.addRowItemCategory('list_input_specs', 'specs_item_hidden');"><img src="/public/images/form/add-icon.png"></a></span>
    <span style="padding-left: 20px;"><a class="cpointer" onclick="AdminController.removeRowItemCategory(this, 'list_input_specs');"><img src="/public/images/form/delete.png"></a></span>
<?php }else if($selectId == 1 && $spectId != 0){ ?>
<span>Nhập tên thông số</span>
    <span><input type="text" name="specs_name[]" class="w200p"></span>
    <span>Chọn kiểu nhập dữ liệu</span>
    <span>
        <select name="specs_input_type[]" id="specs_input_type" class="w200p">
            <option value="1">Selectbox</option>
            <option value="2">Checkbox</option>
            <option value="3">Input</option>
            <option value="4">Radio</option>
        </select>
    </span>
    <span><a class="cpointer" onclick="AdminController.addRowItemCategory('list_input_specs', 'specs_item_hidden');"><img src="/public/images/form/add-icon.png"></a></span>
    <span style="padding-left: 20px;"><a class="cpointer" onclick="AdminController.removeRowItemCategory(this, 'list_input_specs');"><img src="/public/images/form/delete.png"></a></span>
<?php }?>


